<div class="widfat" style="width: 80%;">
    <div class="widget" id="widget_import">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><?php __('Import Student Data');?></h3>
        <div class="content">
            <div class="students import form">
                <!-- Data import form -->
                <?php echo $form->create('Student', array('action'=>'import', 'type'=>'file'));?>
                    <fieldset>
                        <?php e($form->hidden('User.id', array('value'=>'1'))); ?>
                        <?php e($this->element('attachments', array(
                            'controller'=>'users',
                            'plugin' => 'media',
                            'labels' => array(
                                'file' => __('Choose an CSV file that contains student data to import', true),
                                'alternative' => __('short discription of this import, will be saved as import logs', true),
                            )
                        ))); ?>
                        <p><?php e($form->input('User.merge',
                            array('type'=>'checkbox', 'class'=>'checkbox', 'label'=>__('Merge student information that already exists in the database', true)))); ?></p>
                        <br class="clear"/>
                        <?php echo $form->submit(__('Import Selected File', true), array('class'=>'submit attention'));?>
                    </fieldset>
                <?php echo $form->end();?>
                <!-- Print out import logs -->
                <div class="logs">
                    <?php
                        if(!empty($logs)){ ?>
                        <div id="log_togglers">
                            view logs:
                            <a href="javascript:void(0)" id="view_all_log" class="all" title="show all logs">all</a>&nbsp;
                            <a href="javascript:void(0)" id="view_success_log" class="success" title="show success logs">success</a>&nbsp;
                            <a href="javascript:void(0)" id="view_error_log" class="error" title="show error logs">error</a>&nbsp;
                            <a href="javascript:void(0)" id="view_merge_log" class="merge" title="show merge logs">merge</a>&nbsp;
                            <a href="javascript:void(0)" id="view_omit_log" class="omit" title="show omit logs">omit</a>&nbsp;
                        </div>
                        <?php foreach($logs as $log){
                                if(strpos($log, 'Error') !== false){
                                    $class = 'error';
                                }elseif(strpos($log, 'Success') !== false){
                                    $class = 'success';
                                }elseif(strpos($log, 'Merge') !== false){
                                    $class = 'merge';
                                }elseif(strpos($log, 'Omit') !== false){
                                    $class = 'omit';
                                }else{
                                    $class = 'all';
                                }
                                echo "<p class='{$class}'>$log</p>";
                            }
                        }
                    ?>
                </div>
                <?php //debug($data); //debug($logs); ?>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
;jQuery(function(){
    /* Add special label for UserMerge */
    $('label[for=UserMerge]').addClass('checkbox');
    /* Bind log toggler listeners */
    $('#log_togglers a').click(function(){
        var type = $(this).attr('class');
        if(type == 'all'){
            $('div.logs p').show();
        }else{
            $('div.logs p').hide();
            $('div.logs p.'+type).show();
        }
    });
    /* Validate StudentImportForm */
    //$('#StudentImportForm').submit();
    $('#StudentImportForm').submit(function(){
        if(!$(this).valid()) return false;
    }).validate({
        rules: {
            'data[Attachment][0][file]': 'required',
            'data[Attachment][0][alternative]': 'required'
        },
        messages: {
            'data[Attachment][0][file]': '尚未选择文件, 表单无法提交',
            'data[Attachment][0][alternative]': '请填写此次导入的描述, 方便系统记录, 日后备查'
        }
    });
});
</script>